package cn.tianjindong.blog_manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogNoticeMapper;
import cn.tianjindong.blog.pojo.BlogNotice;
import cn.tianjindong.blog.pojo.BlogNoticeExample;
import cn.tianjindong.blog_manager.service.NoticeService;

@Service
public class NoticeServiceImpl implements NoticeService{
	@Autowired
	private BlogNoticeMapper noticeMapper;
	
	@Override
	public void addNotice(BlogNotice notice,Long userId) {
		notice.setManagerId(userId);
		Date date = new Date();
		notice.setCreateTime(date);
		notice.setUpdateTime(date);
		noticeMapper.insert(notice);
	}

	@Override
	public PageView<BlogNotice> getNoticePageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogNotice> list = noticeMapper.getNoticeListLimit(startIndex, countItem);
		// 获取数据库总条数
		int totalItem = noticeMapper.countByExample(new BlogNoticeExample());
		
		PageView<BlogNotice> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		
		return pageView;
	}

	@Override
	public void deleteNoticeById(Long id) {
		noticeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public BlogNotice getNoticeById(Long id) {
		BlogNotice notice = noticeMapper.selectByPrimaryKey(id);
		return notice;
	}

	@Override
	public void updateNotice(BlogNotice notice) {
		Date date = new Date();
		notice.setUpdateTime(date);
		noticeMapper.updateByPrimaryKeySelective(notice);
	}

	@Override
	public void updateNoticeTab(Long id, Integer tab) {
		BlogNotice notice = this.getNoticeById(id);
		if(notice!=null){
			Date date = new Date();
			notice.setUpdateTime(date);
			notice.setTab(tab);
			noticeMapper.updateByPrimaryKeySelective(notice);
		}
	}

}
